package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

import entity.Order;



public class OrderDao extends BaseDao  {

	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;
	
	public int add(Order order) {
		
		int id=0;
    	try {
    		
    		conn = super.getConnection();
    		
    		String sql="insert into easybuy_order(userId,loginName,userAddress,createTime,cost,serialNumber) values(?,?,?,?,?,?) ";
    		
    		pstmt = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
    		
    		pstmt.setObject(1, order.getUserId());
    		pstmt.setObject(2, order.getLoginName());
    		pstmt.setObject(3, order.getUserAddress());
    		pstmt.setObject(4, order.getCreateTime());
    		pstmt.setObject(5, order.getCost());
    		pstmt.setObject(6, order.getSerialNumber());

    		
    		pstmt.executeUpdate();
    		
    		ResultSet rs = pstmt.getGeneratedKeys(); 
			if (rs.next()) { 
				id = rs.getInt(1);
				System.out.println("数据主键：" + id); 
			} 
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
        	super.closeAll(conn, pstmt, rs);
        }
    	return id;
		
	}

}
